﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayCheckMouth : HYExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)3);
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute(ref string tranName)
		{
			tranName = "布置检查口";
			FormLayCheckMouth formLayCheckMouth = new FormLayCheckMouth();
			DialogResult dialogResult = DialogResult.OK;
			if (dialogResult == DialogResult.OK)
			{
				dialogResult = formLayCheckMouth.ShowDialog();
			}
			if (dialogResult == DialogResult.Yes)
			{
				Reference reference = base.Sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RiserPipeFilter(), "请选择立管……");
				Pipe rPipe = base.Doc.GetElement(reference) as Pipe;
				XYZ xyz = base.Sel.PickPoint("请选择检查口开口方向确定点……");
				CheckMouthHelper checkMouthHelper = new CheckMouthHelper(rPipe);
				List<Pipe> list = new List<Pipe>();
				list = checkMouthHelper.GetConnRiserPipe();
				string text = "";
				FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "立管检查口", base.Doc, ref text);
				if (!string.IsNullOrEmpty(text))
				{
					YJKMessageBox.Show(text);
					return Autodesk.Revit.UI.Result.Failed;
				}
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(list[0]);
				XYZ xyz2 = new XYZ(hypiping.StartConnector.Origin.X, hypiping.StartConnector.Origin.Y, 0.0);
				xyz = new XYZ(xyz.X, xyz.Y, 0.0);
				XYZ baseVec = xyz.Subtract(xyz2).Normalize();
				checkMouthHelper.LayCheckMouth(list, familySymbolByName, 1000.0, formLayCheckMouth.GetLayStyle(), baseVec);
				dialogResult = DialogResult.Cancel;
			}
			if (dialogResult == DialogResult.Cancel)
			{
				formLayCheckMouth.Dispose();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
